import 'package:animated_bottom_navigation_bar/animated_bottom_navigation_bar.dart';
import 'package:flutter/material.dart';

import 'package:hockey/pages/jzx_home/jzx_home.dart';
import 'package:hockey/pages/jzx_posts/jzx_posts.dart';
import 'package:hockey/pages/jzx_mine/jzx_mine.dart';
import 'package:hockey/pages/jzx_video/jzx_video.dart';
import 'jzx_apps.dart';

enum TabbarType {
  home,
  video,
  emoji,
  mine;

  Widget get body {
    switch (this) {
      case TabbarType.home:
        return const AutomaticKeepAliveView(child: JzxHomeView());
      case TabbarType.video:
        return const AutomaticKeepAliveView(child: JzxVideoView());
      case TabbarType.emoji:
        return const AutomaticKeepAliveView(child: JzxPostsView());
      case TabbarType.mine:
        return const AutomaticKeepAliveView(child: JzxMineView());
    }
  }
}

class JzxTabbarController extends GetxController {
  static JzxTabbarController get to => Get.find();

  var tabCur = TabbarType.home.obs;

  // final Map<TabbarType, Widget> body = {
  //   TabbarType.home: const JzxHomeView(),
  //   TabbarType.video: const JzxVideoView(),
  //   TabbarType.emoji: const JzxMessageView(),
  //   TabbarType.mine: const JzxMineView(),
  // };

  switchTo(BuildContext context, TabbarType tab) {
    Get.offAllNamed(kRouteTabbar);
    tabCur.value = tab;
  }
}

class TabbarScaffold extends GetView<JzxTabbarController> {
  const TabbarScaffold({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Scaffold(
        resizeToAvoidBottomInset: false,
        extendBody: true,
        body: controller.tabCur.value.body,
        bottomNavigationBar: JzxBlurView(
          width: double.infinity,
          height: Get.bottomBarHeight,
          padding: EdgeInsets.zero,
          // borderRadius: BorderRadius.zero,
          // color: Colors.black.withOpacity(0.6),
          // blur: 20,
          child: AnimatedBottomNavigationBar.builder(
            itemCount: TabbarType.values.length,
            tabBuilder: (index, isActive) {
              final item = TabbarType.values[index];
              return Center(child: _assets(item.name + (isActive ? '_s' : '')));
            },
            backgroundColor: Colors.transparent,
            gapLocation: GapLocation.none,
            activeIndex: controller.tabCur.value.index,
            onTap: (index) {
              switch (index) {
                case 0:
                  JzxLogPvManger.tracking(page: JzxLogPages.home_tab);
                  break;
                case 1:
                  JzxLogPvManger.tracking(page: JzxLogPages.video_tab);
                  break;
                case 2:
                  JzxLogPvManger.tracking(page: JzxLogPages.post_tab);
                  break;
                case 3:
                  JzxLogPvManger.tracking(page: JzxLogPages.mine_tab);
                  break;
                default:
              }
              controller.tabCur.value = TabbarType.values[index];
            },
            //other params
          ),
        ),
      ),
    );
  }
}

Widget _assets(String name) =>
    Image.asset('assets/images/tabs/$name.png', width: 26, height: 26);

/// 独立到每个页面，用脚本生成对应的路由名称
/// 路由名称
///
const String kRouteTabbar = '/tabbar';

/// AppPages : 注册 GetPage
/// 可以直接复制到 AppPages 文件中注册页面路由
///
/// ```
///    GetPage(
///       name: kRouteTabbar,
///       page: () => TabbarScaffold(),
///       binding: TabbarBinding(),
///     ),
/// ```
///
class TabbarBinding extends Binding {
  @override
  List<Bind> dependencies() {
    return [
      Bind.lazyPut<JzxTabbarController>(
        () => JzxTabbarController(),
      ),
      // tabbar 分页懒加载binding
      ...JzxHomeBinding().dependencies(),
      ...JzxVideoBinding().dependencies(),
      ...JzxPostsBinding().dependencies(),
      ...JzxMineBinding().dependencies(),
    ];
  }
}
